package model;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Collections;

/**
 * Created by admin
 * <p>
 * Date :2020/10/13
 * <p>
 * Description :
 * <p>
 * Version :1.0
 */
public class GoldenFlower implements Comparable<GoldenFlower>{
    private static final Logger logger = LoggerFactory.getLogger(GoldenFlower.class);


    public static final int POCKER_COUNT = 3;

    private final PockerCard[] goldenFlower = new PockerCard[POCKER_COUNT];

    private int index = 0;

    public boolean catching(PockerCard pocker){
        if(index < POCKER_COUNT){
            goldenFlower[index] = pocker;
            index++;
            return true;
        }else{
            logger.error("已经发牌完毕");
            return false;
        }
    }

    public boolean isValid(){
        return index == POCKER_COUNT;
    }

    @Override
    public String toString() {
        return Arrays.toString(goldenFlower);
    }

    private int score(){
        int count = (goldenFlower[0].getValue().getPoint()+1) * 100000
                + (goldenFlower[1].getValue().getPoint()+1) * 1000
                + (goldenFlower[2].getValue().getPoint()+1) * 10
                + goldenFlower[0].getType().getType();
        count = count + boomScore() * 9000000 +  combineScore() * 5000000 + colorScore() *3000000 + coupleScore() * 1400000;
        return count;
    }

    /**
     * 是否炸弹
     * @return
     */
    private int boomScore(){
        return goldenFlower[0].getValue() == goldenFlower[2].getValue() ? 1 : 0;
    }

    /**
     * 是否对
     * @return
     */
    private int coupleScore(){
        return goldenFlower[0].getValue() == goldenFlower[1].getValue() ? 1 : 0;
    }

    /**
     * 是否连子
     * @return
     */
    private int combineScore (){
        return (goldenFlower[0].getValue().getPoint() - goldenFlower[2].getValue().getPoint()) == 2 ? 1 : 0;
    }

    private int colorScore(){
        return (goldenFlower[0].getType() ==  goldenFlower[1].getType() &&  goldenFlower[1].getType() ==  goldenFlower[2].getType())?  1 : 0;
    }


    @Override
    public int compareTo(GoldenFlower other) {
        logger.info("排序前...");
        logger.info("您的牌{}", this);
        logger.info("对方的牌{}", other);
        //先对三张牌排序
        Arrays.sort(this.goldenFlower, Collections.reverseOrder());

        Arrays.sort(other.goldenFlower, Collections.reverseOrder());

//        int boomPk = this.boomScore() - other.boomScore();
//        if(boomPk != 0) return boomPk;
//
//        int combinePk = this.combineScore() - other.combineScore();
//        if(combinePk != 0) return boomPk;
//
//        int trumpScore = this.trumpCardScore() - other.trumpCardScore();
//        if(trumpScore != 0) return trumpScore;
        return this.score() - other.score();


//        return 1;
    }
}
